<?php

require_once '../library/Utility/Converter.php'; //dung chuyen doi giua doi tuong va mang (object to array || array to object)

class Application_Models_Menu_Item extends Zend_Db_Table_Abstract {

    protected $_name = 'menu_item';
    protected $_primary = 'menu_item_id';
    
	public function getMetadata()
	{
		if(empty($this->_metadata))
		{
			$this->_setupMetadata();
		}
		return $this->_metadata;
	}
	
    public function _find($menu_item_id) 
    {
        try 
        {
            $row = parent::find($menu_item_id)->current();
            return Utility_Converter::arrayToObject($row->toArray());
        }catch(Zend_Db_Exception $ex) 
        {
            throw $ex;
        }
    }
    
	/**
	 * 
	 * Fetch all menu item of a menu
	 * @param  int $article_id
	 */
	public function fetchMenuItems($menu_id)
	{
		$where = parent::getAdapter()
					->quoteInto('menu_id = ?', $menu_id);
		$menu_items = parent::fetchAll($where, 'left_id');
		return $menu_items;
	}

    public function _update($object) 
    {
        try 
        {
            $arrData = Utility_Converter::objectToArray($object);
            $adapter = parent::getAdapter();
            $where = $adapter->quoteInto('menu_item_id = ?', $object->menu_item_id); 
            parent::update($arrData, $where);
            
        }catch(Zend_Db_Exception $ex) 
        {
            throw $ex;
        }
    }

    public function _insert($object) 
    {
        try 
        {
            $object->menu_item_id = null;
            $arrData = Utility_Converter::objectToArray($object);
            return parent::insert($arrData);
            
        }catch(Zend_Db_Exception $ex) 
        {
            throw $ex;
        }
    }

    public function _delete($menu_item_id) 
    {
        try 
        {
            $where = parent::getAdapter()->quoteInto('menu_item_id = ?', $menu_item_id); 
            return parent::delete($where);
                       
        }catch(Zend_Db_Exception $ex) 
        {
            throw $ex;
        }
    }


}

